package cn.szzsi.business.service;

import cn.szzsi.common.bean.HtmlConfig;
import cn.szzsi.common.bean.SystemConfig;
import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.common.util.SystemConfigUtil;
import cn.szzsi.common.util.TemplateConfigUtil;
import cn.szzsi.model.*;
import com.jfinal.core.JFinal;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.resource.WebAppResourceLoader;

import javax.servlet.ServletContext;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service实现类 - 生成静态
 * 
 */
public class HtmlService {
	
	public static final HtmlService service = new HtmlService();
	
	public void buildHtml(String templateFilePath, String htmlFilePath, Map<String, Object> data) {
		try {
			ServletContext servletContext = JFinal.me().getServletContext();
			WebAppResourceLoader resourceLoader = new WebAppResourceLoader();
			Configuration cfg = Configuration.defaultConfiguration();
			GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
			org.beetl.core.Template template = gt.getTemplate(templateFilePath);
			template.binding(data);

			File htmlFile = new File(servletContext.getRealPath(htmlFilePath));
			File htmlDirectory = htmlFile.getParentFile();
			if (!htmlDirectory.exists()) {
				htmlDirectory.mkdirs();
			}
			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
			template.renderTo(out);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	









	






}
